package com.wtwd.campus.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.annotation.MsgType;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.entity.*;
import com.wtwd.campus.h5_manager.entity.DeviceLog;
import com.wtwd.campus.h5_manager.service.DeviceLogService;
import com.wtwd.campus.service.DeviceInfoService;
import com.wtwd.campus.service.DeviceSettingService;
import com.wtwd.campus.utils.PushUtils;
import com.wtwd.campus.utils.ReceiverUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@MsgType(value = Constants.AUTO_CHANGE_WORK_MODE)
public class AutoChangeWorkModeReceiverHandler implements IReceiverHandler {
    @Autowired
    private DeviceInfoService deviceInfoService;
    @Autowired
    private ReceiverUtils receiverUtils;
    @Autowired
    private DeviceSettingService deviceSettingService;
    @Autowired
    private PushUtils pushUtils;
    @Autowired
    private DeviceLogService deviceLogService;

    @Override
    public void handlerMessage(MqMessage mqMessage) {
        MqAutoChangeModeResult mqAutoChangeModeResult = (MqAutoChangeModeResult) mqMessage.getObject();
        MqSleepTimeAutoChange mqSleepTimeAutoChange = mqAutoChangeModeResult.getMqSleepTimeAutoChange();
        MqLocationData mqLocationData = mqAutoChangeModeResult.getMqLocationData();
        String imei = mqSleepTimeAutoChange.getImei();
        String commandNumber = mqSleepTimeAutoChange.getCommandNumber();
        String start = mqSleepTimeAutoChange.getStart();
        String end = mqSleepTimeAutoChange.getEnd();
        Integer flag = mqSleepTimeAutoChange.getFlag();
        //指令回复的时间
        Long time = System.currentTimeMillis();
        DeviceInfo deviceInfo = deviceInfoService.getDeviceByImei(imei);
        if (deviceInfo != null) {
            if (mqLocationData != null) {
                if (mqLocationData.getLatitude() != 0 && mqLocationData.getLongitude() != 0) {
                    //进行定位数据存储以及围栏告警记录的存储
                    deviceInfo = receiverUtils.dealLocation(mqLocationData, time, deviceInfo, 1);
                }
            }
            //修改设备信息
            deviceInfo.setLastUpTime((new Date()).getTime());
            deviceInfoService.modifyDeviceInfo(deviceInfo);
            //修改设备设置信息
            DeviceSetting deviceSetting = deviceSettingService.getDeviceSettingByImei(imei);
            if (deviceSetting != null) {
                if (!(deviceSetting.getSleepStart().equals(start) && deviceSetting.getSleepEnd().equals(end) && deviceSetting.getSleepSwitch() == flag)) {
                    deviceSetting.setSleepStart(start);
                    deviceSetting.setSleepEnd(end);
                    deviceSetting.setSleepSwitch(flag);
                    deviceSettingService.modifyDeviceSetting(deviceSetting);
                }
            }
            //删除缓存中的指令
            String delKey = Constants.AUTO_CHANGE_WORK_MODE.concat("-").concat(commandNumber);
            receiverUtils.deleteCommandInRedis(imei, delKey);
            //完善指令下发记录
            receiverUtils.completeCommandRecord(Constants.AUTO_CHANGE_WORK_MODE, imei, commandNumber, null, Constants.COMMAND_COMPLETE, time);
            //存储设备接收日志
            DeviceLog deviceLog = new DeviceLog();
            deviceLog.setCommandNumber(commandNumber);
            deviceLog.setSendUser(Constants.DEVICE_USER);
            deviceLog.setImei(imei);
            deviceLog.setBigType(Constants.MESS_TYPE);
            deviceLog.setType(Constants.AUTO_CHANGE_WORK_MODE);
            JSONObject text = new JSONObject();
            text.put("设备号",imei);
            text.put("睡眠模式开始时间",mqSleepTimeAutoChange.getStart());
            text.put("睡眠模式结束时间",mqSleepTimeAutoChange.getEnd());
            text.put("功能的开关 0-关闭 1-开启",flag);
            text.put("经度",mqLocationData.getLongitude());
            text.put("纬度",mqLocationData.getLatitude());
            deviceLog.setCmd(text.toJSONString());
            deviceLog.setSendTime(System.currentTimeMillis());
            deviceLog.setState(1);
            deviceLogService.addDeviceLog(deviceLog);
        }else{
            saveUnInStorage(imei);
            receiverUtils.cloaseChannel(imei);
        }
    }

}
